<?php

namespace app\mp\admin;

use app\system\admin\Admin;

/**
 * 成本管理》核价跟进
 */
class PriceDiscuss extends Admin
{

    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
                ['ext.special_process_a_name', '=', '染色'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_index($where);
            $order = "ext.special_process_a_unitprice asc, o.create_time desc";
            $list = db('order_ext ext ')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.id as order_id,o.client_ordernum ,pdis.type
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,ext.special_process_a_name,ext.special_process_a_unitprice
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id
                ,s_user.username
                ')
                ->join('order o', 'o.id = ext.order_id', 'left')
                ->join('price_discuss pdis', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)
                ->where(' ( pdis.status is null or pdis.status < 2 ) ')
                ->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('order_ext ext ')
                ->join('order o', 'o.id = ext.order_id', 'left')
                ->join('price_discuss pdis', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)
                ->where(' ( pdis.status is null or pdis.status < 2 ) ')
                ->count();
            $list_last = [];
            foreach ($list as $k => $v) {
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['order_status_txt'] = $this->_get_order_status_txt($v);
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    //获取订单状态说明
    private function _get_order_status_txt($v)
    {
        $v['status'] = $v['order_status'];

        if ($v['status'] == 99) {
            return '已废弃';
        }
        if ($v['status'] == 99) {
            return 'MES废除';
        }
        if ($v['caigou_status'] == 0) {
            return '未采购';
        }
        if ($v['caigou_status'] == 1 && $v['caichuang_status'] == 0) {
            return '已采购';
        }
        if ($v['caichuang_status'] == 1 && $v['chejian_status'] == 0) {
            return '已裁床';
        }
        if ($v['chejian_status'] == 1 && $v['tail_status'] == 0) {
            return '已车缝';
        }
        if ($v['tail_status'] == 1 && $v['real_shipment_status'] == 0) {
            return '已后整';
        }
        if ($v['real_shipment_status'] == 1 && $v['status'] == 1) {
            return '已出货';
        }
        if ($v['status'] == 2) {
            return '已完成';
        }
        return '新单未处理';
    }

    private function _where_index($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $createDateRange = $this->request->param('create_date_range/s');//2021-12-01 ~ 2021-12-11
        $createDateRange = '2021-12-01 ~ ' . date('Y-m-d');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);
        $special_process_a_unitprice = floatval($this->request->param('special_process_a_unitprice', 0));

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($createDateRange != '') {
            list($startTime, $endTime) = array_map('strtotime', explode('~', $createDateRange));
            $where[] = ['o.create_time', 'between', [$startTime, $endTime + 86399]];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        if ($special_process_a_unitprice > 0) {
            $where[] = ['ext.special_process_a_unitprice', '=', $special_process_a_unitprice];
        }
        return $where;
    }

    /**
     * 审核
     * @date 2021/12/8
     * @author zhuguosen 905502314@qq.com
     */
    public function audit()
    {
        $status = $this->request->param('status/d', 0);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        switch ($status) {
            case 0:
            case 1:
                $info = db('price_discuss pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'special_process_a_name_yanse'],
                ])->find();
                if (!isset($info['id'])) {
                    $orderInfo = db('order o')
                        ->field('o.id,o.client_ordernum 
                ,ext.special_process_a_name,ext.special_process_a_unitprice ')
                        ->join('order_ext ext', 'o.id = ext.order_id', 'left')
                        ->where([
                            ['o.client_ordernum', '=', $clientOrdernum],
                        ])->find();
                    $temp = db('price_discuss')->insert([
                        'order_id' => $orderInfo['id'],
                        'client_ordernum' => $orderInfo['client_ordernum'],
                        'type' => 'special_process_a_name_yanse',
                        'status' => 2,
                        'special_process_a_name' => trim($orderInfo['special_process_a_name']),
                        'special_process_a_unitprice' => floatval($orderInfo['special_process_a_unitprice']),
                        'price_0' => 0,
                        'price_1' => 0,
                        'admin_id' => ADMIN_ID,
                        'create_time' => time(),
                        'update_time' => time(),
                    ]);
                    if ($temp == 1) {
                        return $this->json(1, '审核完成', $info);
                    }
                    return $this->json(0, '审核失败');
                }
                $info = db('price_discuss pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'special_process_a_name_yanse'],
                    ['status', '=', 1]
                ])->update([
                    'status' => 2,
                    'admin_id' => ADMIN_ID,
                    'update_time' => time(),
                ]);
                return $this->json(1, '审核完成', $info);
                break;
            case 2:
                $info = db('price_discuss pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'special_process_a_name_yanse'],
                    ['status', '=', 2]
                ])->update([
                    'status' => 100,
                    'admin_id' => ADMIN_ID,
                    'update_time' => time(),
                ]);
                return $this->json(1, '审核完成', $info);
                break;
        }
        return $this->json(0, '审核失败');
    }

    public function indexRuning()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
                ['pdis.status', '=', 2],
                ['pdis.type', '=', 'special_process_a_name_yanse'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_indexRuning($where);
            $order = "pdis.id desc";
            $list = db('price_discuss pdis')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,pdis.order_id,pdis.client_ordernum  ,pdis.type
                ,pdis.special_process_a_name,pdis.special_process_a_unitprice 
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id 
                ,s_user.username
                ')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('price_discuss pdis')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->where($where)->count();
            $list_last = [];
            foreach ($list as $k => $v) {
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['order_status_txt'] = $this->_get_order_status_txt($v);
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    private function _where_indexRuning($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        return $where;
    }

    public function indexFinish()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
                ['pdis.status', '=', 100],
                ['pdis.type', '=', 'special_process_a_name_yanse'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_indexFinish($where);
            $order = "pdis.id desc";
            $list = db('price_discuss pdis')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,pdis.order_id,pdis.client_ordernum  ,pdis.type
                ,pdis.special_process_a_name,pdis.special_process_a_unitprice 
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id 
                ,s_user.username
                ')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('price_discuss pdis')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->where($where)->count();
            $list_last = [];
            foreach ($list as $k => $v) {
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['order_status_txt'] = $this->_get_order_status_txt($v);
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    private function _where_indexFinish($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        return $where;
    }

    /**
     * 仓库管理》染色管理
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/16
     */
    public function index_ranse()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
//                ['ext.special_process_a_name', '=', '染色'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_index_ranse($where);
            $order = "ext.special_process_a_unitprice asc, o.create_time desc";
            $list = db('order_ext ext ')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.id as order_id,o.client_ordernum ,pdis.type
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,ext.special_process_a_name,ext.special_process_a_unitprice
                ,ext.special_process_b_name,ext.special_process_b_unitprice
                ,ext.special_process_c_name,ext.special_process_c_unitprice
                ,ext.special_process_d_name,ext.special_process_d_unitprice
                ,ext.special_process_e_name,ext.special_process_e_unitprice
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id,pdis.remark
                ,s_user.username
                ')
                ->join('order o', 'o.id = ext.order_id', 'left')
                ->join('price_discuss_ext pdis', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->whereRaw("ext.special_process_a_name='染色' OR ext.special_process_b_name='染色'
                  OR ext.special_process_c_name='染色' OR ext.special_process_d_name='染色'
                  OR ext.special_process_e_name='染色' ")
                ->where($where)
                ->where(" ( pdis.id is null or (pdis.status < 2) ) ")
                ->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('order_ext ext ')
                ->join('order o', 'o.id = ext.order_id', 'left')
                ->join('price_discuss_ext pdis', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->whereRaw("ext.special_process_a_name='染色' OR ext.special_process_b_name='染色'
                  OR ext.special_process_c_name='染色' OR ext.special_process_d_name='染色'
                  OR ext.special_process_e_name='染色' ")
                ->where($where)
                ->where(" ( pdis.id is null or (pdis.status < 2) ) ")
                ->count();
            $list_last = [];
            $this->material_status_get($list);
            foreach ($list as $k => $v) {
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                //染色信息处理
                $special_process_res = $this->_get_special_process_res($v);
                $v['special_process_res_type'] = $special_process_res['special_process_res_type'];
                $v['special_process_res_name'] = $special_process_res['special_process_res_name'];
                $v['special_process_res_unitprice'] = $special_process_res['special_process_res_unitprice'];
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    private function _get_special_process_res($v)
    {
        $special_process_res = [];
        if ($v['special_process_a_name'] == '染色') {
            $special_process_res['special_process_res_type'] = 'a';
            $special_process_res['special_process_res_name'] = $v['special_process_a_name'];
            $special_process_res['special_process_res_unitprice'] = $v['special_process_a_unitprice'];
            return $special_process_res;
        }
        if ($v['special_process_b_name'] == '染色') {
            $special_process_res['special_process_res_type'] = 'b';
            $special_process_res['special_process_res_name'] = $v['special_process_b_name'];
            $special_process_res['special_process_res_unitprice'] = $v['special_process_b_unitprice'];
            return $special_process_res;
        }
        if ($v['special_process_c_name'] == '染色') {
            $special_process_res['special_process_res_type'] = 'c';
            $special_process_res['special_process_res_name'] = $v['special_process_c_name'];
            $special_process_res['special_process_res_unitprice'] = $v['special_process_c_unitprice'];
            return $special_process_res;
        }
        if ($v['special_process_d_name'] == '染色') {
            $special_process_res['special_process_res_type'] = 'd';
            $special_process_res['special_process_res_name'] = $v['special_process_d_name'];
            $special_process_res['special_process_res_unitprice'] = $v['special_process_d_unitprice'];
            return $special_process_res;
        }
        if ($v['special_process_e_name'] == '染色') {
            $special_process_res['special_process_res_type'] = 'e';
            $special_process_res['special_process_res_name'] = $v['special_process_e_name'];
            $special_process_res['special_process_res_unitprice'] = $v['special_process_e_unitprice'];
            return $special_process_res;
        }
        return $special_process_res;
    }

    private function _where_index_ranse($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $createDateRange = $this->request->param('create_date_range/s');//2021-12-01 ~ 2021-12-11
        $createDateRange = '2022-05-01 ~ ' . date('Y-m-d');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);
        $special_process_a_unitprice = floatval($this->request->param('special_process_a_unitprice', 0));

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($createDateRange != '') {
            list($startTime, $endTime) = array_map('strtotime', explode('~', $createDateRange));
            $where[] = ['o.create_time', 'between', [$startTime, $endTime + 86399]];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        if ($special_process_a_unitprice > 0) {
            $where[] = ['ext.special_process_a_unitprice', '=', $special_process_a_unitprice];
        }
        return $where;
    }

    /**
     * 仓库管理》染色管理 修改字段
     * @date 2021/12/25
     * @author zhuguosen 905502314@qq.com
     */
    public function editUnit_ranse()
    {
        $param = [
            'client_ordernum' => $this->request->param('client_ordernum/s', ''),
            'type' => $this->request->param('type/s', 'yanse_manage'),
            'field' => $this->request->param('field/s', 0),
            'value' => $this->request->param('value/s', 0),
        ];
        $info = db('price_discuss_ext pdis')->where([
            ['client_ordernum', '=', $param['client_ordernum']],
            ['type', '=', $param['type']],
        ])->find();
        if (!isset($info['id'])) {
            $orderInfo = db('order o')
                ->field('o.id,o.client_ordernum 
                ,ext.special_process_a_name,ext.special_process_a_unitprice ')
                ->join('order_ext ext', 'o.id = ext.order_id', 'left')
                ->where([
                    ['o.client_ordernum', '=', $param['client_ordernum']],
                ])->find();
            $temp = db('price_discuss_ext')->insert([
                'order_id' => $orderInfo['id'],
                'client_ordernum' => $orderInfo['client_ordernum'],
                'type' => $param['type'],
                'status' => 0,
                'special_process_a_name' => trim($orderInfo['special_process_a_name']),
                'special_process_a_unitprice' => floatval($orderInfo['special_process_a_unitprice']),
                'price_0' => 0,
                'price_1' => 0,
                'admin_id' => ADMIN_ID,
                'create_time' => time(),
                'update_time' => time(),
            ]);
        }
        //修改
        $temp = db('price_discuss_ext')->where([
            ['client_ordernum', '=', $param['client_ordernum']],
            ['type', '=', $param['type']],
        ])->update([
            $param['field'] => $param['value'],
        ]);
        if ($temp == 1) {
            return $this->Json(1, '修改成功', $temp);
        }
        return $this->Json(10, '修改失败', $temp);
    }

    /**
     * 仓库管理》染色管理  进行中
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/16
     */
    public function indexRuning_ranse()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
                ['pdis.status', '=', 2],
                ['pdis.type', '=', 'yanse_manage'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_indexRuning_ranse($where);
            $order = "pdis.id desc";
            $list = db('price_discuss_ext pdis')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,pdis.order_id,pdis.client_ordernum  ,pdis.type
                ,pdis.special_process_res_type,pdis.special_process_res_name,pdis.special_process_res_unitprice 
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id ,pdis.remark
                ,pdis.update_time
                ,s_user.username
                ')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('price_discuss_ext pdis')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->count();
            $list_last = [];
            $this->material_status_get($list);
            foreach ($list as $k => $v) {
                $v['update_time'] = $v['update_time'] > 0 ? date('Y-m-d H:i:s', $v['update_time']) : '';
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    private function _where_indexRuning_ranse($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        return $where;
    }

    /**
     * 仓库管理》染色管理  已完成
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/16
     */
    public function indexFinish_ranse()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0) || $this->request->param('ajaxflag/d', 0) == 1) {
            $where = [
                ['pdis.status', '=', 100],
                ['pdis.type', '=', 'yanse_manage'],
            ];
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->_where_indexFinish_ranse($where);
            $order = "pdis.id desc";
            $list = db('price_discuss_ext pdis')
                ->field('ft.name as ft_name
                ,o.sku ,o.is_auto_import,o.create_time
                ,o.caigou_status,o.caichuang_status,o.chejian_status,o.tail_status,o.real_shipment_status,o.status as order_status ,o.client_status
                ,pdis.order_id,pdis.client_ordernum  ,pdis.type
                ,pdis.special_process_res_type,pdis.special_process_res_name,pdis.special_process_res_unitprice 
                ,pdis.id as pdis_id,pdis.status,pdis.price_0,pdis.price_1,pdis.admin_id,pdis.remark
                ,pdis.update_time
                ,s_user.username
                ')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->page($page)->limit($limit)->orderRaw($order)
                ->select();
            $count = db('price_discuss_ext pdis')
                ->join('order o', 'o.id = pdis.order_id', 'left')
                ->join('order_ext ext', 'pdis.order_id = ext.order_id', 'left')
                ->join('factory ft', "ft.id=o.factory_id", 'left')
                ->join('system_user s_user', 'pdis.admin_id= s_user.id', 'left')
                ->where($where)->count();
            $list_last = [];
            $this->material_status_get($list);
            foreach ($list as $k => $v) {
                $v['update_time'] = $v['update_time'] > 0 ? date('Y-m-d H:i:s', $v['update_time']) : '';
                $v['create_time'] = $v['create_time'] > 0 ? date('Y-m-d H:i:s', $v['create_time']) : '';
                $v['is_auto_import_txt'] = '';
                switch ($v['is_auto_import']) {
                    case 1:
                        $v['is_auto_import_txt'] = '小辉辉';
                        break;
                    case 2:
                        $v['is_auto_import_txt'] = '名贵妨';
                        break;
                    case 3:
                        $v['is_auto_import_txt'] = '十义';
                        break;
                }
                $list_last["_{$v['client_ordernum']}"] = $v;
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list_last,
                'count' => $count,
            ];
            return json($result);
        }
        $assign = [];
        return $this->assign($assign)->fetch();
    }

    private function _where_indexFinish_ranse($where)
    {
        $page = $this->request->param('page/d', 1);
        $limit = $this->request->param('limit/d', 20);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        $sku = $this->request->param('sku/s', '');
        $orderStatus = $this->request->param('order_status/s');
        $isAutoImport = $this->request->param('is_auto_import/d', 0);

        //订单加一级
        order_level($where, 'o.');
        if ($clientOrdernum == '') {
            $where[] = ['o.status', '<>', 99];
        }
        if ($clientOrdernum != '') {
            $where[] = ['o.client_ordernum', 'like', "%" . $clientOrdernum . "%"];
        }
        if ($sku != '') {
            $where[] = ['o.sku', 'like', "%" . $sku . "%"];
        }
        if ($isAutoImport > 0) {
            $where[] = ['o.is_auto_import', '=', $isAutoImport];
        }
        if ($orderStatus == 99) $where[] = ['o.status', '=', 99];
        elseif ($orderStatus == 999) $where[] = ['o.client_status', '=', 99];
        if ($orderStatus != '') {
            switch ($orderStatus) {
                case 1:
                    $where[] = ['o.caigou_status', '=', 0];
                    break;
                case 2:
                    $where[] = ['o.caigou_status', '=', 1];
                    $where[] = ['o.caichuang_status', '=', 0];
                    break;
                case 3:
                    $where[] = ['o.caichuang_status', '=', 1];
                    $where[] = ['o.chejian_status', '=', 0];
                    break;
                case 4:
                    $where[] = ['o.chejian_status', '=', 1];
                    $where[] = ['o.tail_status', '=', 0];
                    break;
                case 5:
                    $where[] = ['o.tail_status', '=', 1];
                    $where[] = ['o.real_shipment_status', '=', 0];
                    break;
                case 6:
                    $where[] = ['o.real_shipment_status', '=', 1];
                    $where[] = ['o.status', '=', 1];
                    break;
                case 7:
                    $where[] = ['o.status', '=', 2];
                    break;
            }
        }
        return $where;
    }

    /**
     * 审核 --仓库管理》染色管理
     * @date 2022/6/16
     * @author zhuguosen 905502314@qq.com
     */
    public function audit_ranse()
    {
        $status = $this->request->param('status/d', 0);
        $clientOrdernum = $this->request->param('client_ordernum/s', '');
        switch ($status) {
            case 0:
            case 1:
                $info = db('price_discuss_ext pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'yanse_manage'],
                ])->find();
                if (!isset($info['id'])) {
                    $orderInfo = db('order o')
                        ->field('o.id,o.client_ordernum 
                ,ext.special_process_a_name,ext.special_process_a_unitprice
                ,ext.special_process_b_name,ext.special_process_b_unitprice
                ,ext.special_process_c_name,ext.special_process_c_unitprice
                ,ext.special_process_d_name,ext.special_process_d_unitprice
                ,ext.special_process_e_name,ext.special_process_e_unitprice
               ')
                        ->join('order_ext ext', 'o.id = ext.order_id', 'left')
                        ->where([
                            ['o.client_ordernum', '=', $clientOrdernum],
                        ])->find();
                    $special_process_res = $this->_get_special_process_res($orderInfo);
                    $v['special_process_res_type'] = $special_process_res['special_process_res_type'];
                    $v['special_process_res_name'] = $special_process_res['special_process_res_name'];
                    $v['special_process_res_unitprice'] = $special_process_res['special_process_res_unitprice'];

                    $temp = db('price_discuss_ext')->insert([
                        'order_id' => $orderInfo['id'],
                        'client_ordernum' => $orderInfo['client_ordernum'],
                        'type' => 'yanse_manage',
                        'status' => 0,
                        'special_process_a_name' => trim($orderInfo['special_process_a_name']),
                        'special_process_a_unitprice' => floatval($orderInfo['special_process_a_unitprice']),
                        'special_process_res_type' => $special_process_res['special_process_res_type'],
                        'special_process_res_name' => $special_process_res['special_process_res_name'],
                        'special_process_res_unitprice' => $special_process_res['special_process_res_unitprice'],
                        'price_0' => 0,
                        'price_1' => 0,
                        'admin_id' => ADMIN_ID,
                        'create_time' => time(),
                        'update_time' => time(),
                    ]);
                }
                $info = db('price_discuss_ext pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'yanse_manage'],
                ])->update([
                    'status' => 2,
                    'admin_id' => ADMIN_ID,
                    'update_time' => time(),
                ]);
                return $this->json(1, '审核完成', $info);
                break;
            case 2:
                $info = db('price_discuss_ext pdis')->where([
                    ['client_ordernum', '=', $clientOrdernum],
                    ['type', '=', 'yanse_manage'],
                    ['status', '=', 2]
                ])->update([
                    'status' => 100,
                    'admin_id' => ADMIN_ID,
                    'update_time' => time(),
                ]);
                return $this->json(1, '审核完成', $info);
                break;
        }
        return $this->json(0, '审核失败');
    }

    /**
     * 仓库管理》染色管理  设置 物料状态
     * @param $list
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/16
     */
    private function material_status_get(&$list)
    {
        if (count($list) <= 0) {
            return true;
        }
        $order_id_array = [];
        foreach ($list as $k => $v) {
            $order_id_array[] = $v['order_id'];
        }
        $OrderMaterialService = new \app\common\service\OrderMaterial();
        $list_order_material = $OrderMaterialService->get_order_material_list($order_id_array);
        foreach ($list as $k => $v) {
            $list[$k]['omList'] = [];
            foreach ($list_order_material as $k2 => $sv) {
                if ($v['order_id'] == $sv['order_id'] && $sv['type'] == 'fabric') {
                    // 物料状态
                    $list[$k]['material_status_txt'] = '';
                    switch ($sv['material_status']['is_caigou_start']) {
                        case 0:
                            $list[$k]['material_status_txt'] = '开始采购';
                            break;
                        case 1:
                            $list[$k]['material_status_txt'] = '采购中';
                            $sv['arrive_time'] > 0 && $list[$k]['material_status_txt'] .= '-已到料';
                            break;
                        case 2:
                            $list[$k]['material_status_txt'] = '采购完成';
                            break;
                    }
                    switch ($sv['tiaoshu_status']) {
                        case 10:
                            $list[$k]['material_status_txt'] = '换料中';
                            break;
                        case 11:
                            $list[$k]['material_status_txt'] = '换料完成';
                            break;
                    }
                    if ($sv['caichuang_can_do'] == 1) {
                        $list[$k]['material_status_txt'] = '可裁剪';
                    }
                    break;
                }
            }
        }

    }

}